<?php


namespace app\backend\controller\v5;

use app\backend\validate\RecordValidate;
use app\common\models\Record as RecordModel;
use think\exception\ValidateException;
use app\common\models\ApiMember;
use app\common\models\DatumCate;

class Record extends BaseController
{
    /**
     * 记录表管理
     *
     * @return \think\Response
     */
    public function index()
    {
        // 记录表
        // where条件
//        $where = input('datum_title_id');
        // 分页、排序等
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 10);
        $list = RecordModel::field('*')
            ->order('id', 'asc')
//            ->where(function ($query) use ($where) {
//                $query->where('datum_title_id', 'like', '%' . $where . '%');
//            })
            ->select();

        foreach ($list as &$value) {
            $value['api_member_id'] = $value->apimember->nickname;
            $value['backend_member_id'] = $value->backmember->username;
            $value['datum_cate_id'] = $value->datumcate->title;
            $value['datum_id'] = $value->datum->title;
            unset($value->apimember);
            unset($value->backmember);
            unset($value->datumcate);
            unset($value->datum);
        }
        //获取列表
        return sucessMsg(200, '获取成功！', ['list' => $list, 'total' => count($list), 'page' => $page, 'pageSize' => $pageSize]);
    }

    /**
     * 添加
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = new RecordModel();
        $data = input();
        try {
            validate(RecordValidate::class)->scene('create')->check($data);
            if ($user->save($data)) {
                return sucessMsg(200, '创建成功！');
            }
            return errMsg(500, '创建失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(400, '请选择要修改的id！');
        }
        $data = input();
        try {
            validate(RecordValidate::class)->scene('update')->check($data);
            if (RecordModel::update($data, ['id' => $id])) {
                return sucessMsg(200, '修改成功！');
            }
            return errMsg(500, '修改失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            return errMsg(400, '请选择要删除数据的id！');
        }
        $data = RecordModel::find($id);
        if (empty($data)) {
            return errMsg(400, '要删除的数据为空！');
        }
        if (RecordModel::destroy($id)) {
            return sucessMsg(200, '删除成功！');
        }
        return errMsg(500, '删除失败！');
    }

    /*
     * 进度
     */
    public function location()
    {
        $api_member_id = input('api_member_id/d');
        $model = new RecordModel();
        $data = $model->where(['status' => 1, 'api_member_id' => $api_member_id])
            ->field('id,api_member_id,backend_member_id,datum_id,datum_cate_id')
            ->order('datum_cate_id', 'desc')
            ->find();
        if (empty($data)) {
            return errMsg(400, '暂无进度', $data);
        }
        $model1 = new DatumCate();
        $arr = $model1->where('id', $data['datum_cate_id'])->field('grade_id,course_id')->find();
        if (!empty($arr)) {
            $arr['grade_id'] = $arr->grade->title;
            $arr['course_id'] = $arr->course->title;
            unset($arr->grade);
            unset($arr->course);
        }
        return sucessMsg(200, '获取成功', ['data' => $arr]);
    }

    /*
     * 教师
     */
    public function teacher()
    {
        $id = input('backend_member_id/d');
        $model = new ApiMember();
        $data = $model
            ->where(['status' => 1, 'backend_member_id' => $id])
            ->field('id,sex,nickname,account,backend_member_id')
            ->select();
        foreach ($data as $value) {
            $value['backend_member_id'] = $value->backend->username;
            unset($value->backend);
        }
        return sucessMsg(200, '获取成功', ['data' => $data]);
    }

}